package lifeOfAFish.scenegraph.geom;

import java.io.File;

import javax.media.opengl.GL;

import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;

import lifeOfAFish.util.MeshLoader;
import lifeOfAFish.scenegraph.*;

public class sgGeomMesh extends sgGeometry {
	
	//
	// Fields
	// 
	private int m_displayList;
	private String m_filename = null;
	private String m_textureName = null;
	
	Texture m_tex = null;

	//
	// Constructors
	//
	public sgGeomMesh () 
	{
		m_displayList = -1;
	};

	//
	// Methods
	//


	//
	// Accessor methods
	//

	//
	// Other methods
	//

	public void setOBJFileName(String name){
		m_filename = name;
	}
	
	public void setTextureFileName(String name){
		m_textureName = name;
	}

	public void load()
	{
		if(m_filename == null) return;
		m_displayList = MeshLoader.loadObj(sgGlobals.gl, m_filename);
		
		// load the trout texture
		if(m_textureName == null) return;
		try {
	     m_tex = TextureIO.newTexture(new File(m_textureName), false);
		}catch (Exception e)
		{
			System.out.println("error loading texture: "+m_textureName);
		}
	}

	/**
	 * Render the model, optional binding texture if necessary
	 */
	protected void draw()
	{
		GL gl = sgGlobals.gl;
		if(m_tex != null) {m_tex.enable(); m_tex.bind();}
		gl.glCallList(m_displayList);
		if(m_tex != null) m_tex.disable();
	};
	
	/**
	 * Return (local) bounding box
	 */
	public float[] getBoundingBox(){
		// stub for now
		float[] bb = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
		return bb;
	}
	
}
